package cn.shiwei.pet.service.impl;

import cn.shiwei.basic.exception.MyException;
import cn.shiwei.basic.service.impl.BaseServiceImpl;
import cn.shiwei.pet.domain.Pet;
import cn.shiwei.pet.mapper.PetDetailMapper;
import cn.shiwei.pet.mapper.PetMapper;
import cn.shiwei.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        petMapper.batchPutStatus(Pet.ADDED_TIME ,date ,Pet.ON_SALE_STATUS, ids);

    }

    @Override
    public void offsale(List<Long> ids) {
        Date date = new Date();
        petMapper.batchPutStatus(Pet.OFF_SHELF_TIME ,date ,Pet.UN_SOLD_STATUS, ids);
    }

    @Override
    public Pet loadPetAndDetailByIDState(Long id, Integer state) {
        Pet pet = petMapper.loadPetAndDetailByIDState(id, state);
        return pet;
    }

    @Override
    public Pet findById(Long id) {
        Pet pet = super.findById(id);
        if (!Pet.ON_SALE_STATUS.equals(pet.getState())){
            throw new MyException("非法访问");
        }
        return pet;
    }
}
